﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AMatysiak.Controls;
using System.Drawing;

namespace AMatysiakTest.Controls
{
    [TestClass]
    public class InvoiceGridTest
    {
        private string[] vatArray = new string[] { "zw.", "0%", "7%", "22%" };

        [TestMethod]
        public void createInstance()
        {
            InvoiceGrid grid = new InvoiceGrid();
            Assert.IsInstanceOfType(grid, typeof(InvoiceGrid));
        }

        [TestMethod]
        public void enabled()
        {
            InvoiceGrid grid = new InvoiceGrid();

            grid.Enabled = true;
            Assert.IsTrue(grid.Enabled);

            grid.Enabled = false;
            Assert.IsFalse(grid.Enabled);
        }

        [TestMethod]
        public void backColor()
        {
            InvoiceGrid grid = new InvoiceGrid();

            Color backColor = grid.BackColor;
            Assert.AreEqual(backColor, grid.BackColor);

            grid.BackColor = Color.White;
            Assert.AreEqual(Color.White, grid.BackColor);
        }

        [TestMethod]
        public void vats()
        {
            InvoiceGrid grid = new InvoiceGrid();

            grid.Vats = vatArray;
            Assert.AreEqual(vatArray, grid.Vats);
        }

        [TestMethod]
        public void decrets()
        {
            InvoiceGrid grid = new InvoiceGrid();

            InvoiceGrid.GridDecret[] decrets = new InvoiceGrid.GridDecret[]
            {
                new InvoiceGrid.GridDecret()
                {
                    Name = "test",
                    Multiplier = 1,
                    Netto = 100,
                    Vat = "22%"
                }
            };

            grid.Decrets = decrets;
            decrets[0].Brutto = 122;
            Assert.AreEqual(decrets[0], grid.Decrets[0]);
        }

        [TestMethod]
        public void decretsCorrect()
        {
            InvoiceGrid grid = new InvoiceGrid();
            grid.Vats = vatArray;

            InvoiceGrid.GridDecret[] decrets = new InvoiceGrid.GridDecret[]
            {
                new InvoiceGrid.GridDecret()
                {
                    Name = "test",
                    Multiplier = 1,
                    Netto = 100,
                    Brutto = 122,
                    Vat = "22%"
                }
            };

            grid.Decrets = decrets;

            bool valid = grid.DecretsCorrect;
            Assert.IsTrue(valid);
        }

        [TestMethod]
        public void decretsIncorrect()
        {
            InvoiceGrid grid = new InvoiceGrid();
            grid.Vats = vatArray;

            InvoiceGrid.GridDecret[] decrets = new InvoiceGrid.GridDecret[]
            {
                new InvoiceGrid.GridDecret()
                {
                    Name = "",
                    Multiplier = 1,
                    Netto = 100,
                    Brutto = 122,
                    Vat = "22%"
                }
            };

            grid.Decrets = decrets;
            Assert.IsFalse(grid.DecretsCorrect, "name fault");

            decrets = new InvoiceGrid.GridDecret[]
            {
                new InvoiceGrid.GridDecret()
                {
                    Name = "test",
                    Multiplier = 1,
                    Netto = 100,
                    Brutto = 122,
                    Vat = ""
                }
            };
            grid.Decrets = decrets;
            Assert.IsFalse(grid.DecretsCorrect, "vat fault");
        }

        [TestMethod]
        public void showUnit()
        {
            InvoiceGrid grid = new InvoiceGrid();
            grid.Vats = vatArray;

            InvoiceGrid.GridDecret[] decrets = new InvoiceGrid.GridDecret[]
            {
                new InvoiceGrid.GridDecret()
                {
                    Name = "test",
                    Multiplier = 1,
                    Unit = "m3",
                    Netto = 100,
                    Brutto = 122,
                    Vat = "22%"
                }
            };

            grid.Decrets = decrets;

            grid.ShowUnit = false;
            Assert.AreEqual("", grid.Decrets[0].Unit);

            grid.ShowUnit = true;
            Assert.AreEqual("m3", grid.Decrets[0].Unit);
        }

        [TestMethod]
        public void showPKWiU()
        {
            InvoiceGrid grid = new InvoiceGrid();
            grid.Vats = vatArray;

            InvoiceGrid.GridDecret[] decrets = new InvoiceGrid.GridDecret[]
            {
                new InvoiceGrid.GridDecret()
                {
                    Name = "test",
                    Multiplier = 1,
                    PKWiU = "123/23/23",
                    Netto = 100,
                    Brutto = 122,
                    Vat = "22%"
                }
            };

            grid.Decrets = decrets;

            grid.ShowPKWiU = false;
            Assert.AreEqual("", grid.Decrets[0].PKWiU);

            grid.ShowPKWiU = true;
            Assert.AreEqual("123/23/23", grid.Decrets[0].PKWiU);
        }

        [TestMethod]
        public void valueChanged()
        {
            double netto = 0;
            double brutto = 0;

            InvoiceGrid grid = new InvoiceGrid();
            grid.ChangedValueColumns += delegate(double n, double b) { netto = n; brutto = b; };

            InvoiceGrid.GridDecret[] decrets = new InvoiceGrid.GridDecret[]
            {
                new InvoiceGrid.GridDecret()
                {
                    Name = "test",
                    Multiplier = 1,
                    Netto = 100,
                    Vat = "22%"
                }
            };

            grid.Decrets = decrets;

            Assert.AreEqual(100, netto);
            Assert.AreEqual(122, brutto);
        }

        [TestMethod]
        public void raiseActualization()
        {
            double netto = 10;
            double brutto = 10;

            InvoiceGrid grid = new InvoiceGrid();
            grid.ChangedValueColumns += delegate(double n, double b) { netto = n; brutto = b; };

            grid.RaiseActualizeStatistics();

            Assert.AreEqual(0, netto);
            Assert.AreEqual(0, brutto);
        }

    }
}
